package com.xisoft.generator.utils;

import cn.hutool.json.JSONObject;
import java.util.LinkedHashMap;
import java.util.Map;

/************************************************************* <br>
 *
 *说明:返回数据 <br>
 *
 * @Description   <br>
 * @Date Created on 2021/11/17  12:56  <br>
 * @return   <br>
 * @Author LIBENCHENG
 * @version 1.0 <br>
 ************************************************************* <br>
 */
public class ResultEntity extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public ResultEntity() {
        put("code", 0);
        put("msg", "请求成功!");
    }

    public static ResultEntity error() {
        return error(-1, "未知异常，请联系管理员");
    }

    public static ResultEntity error(String msg) {
        return error(-1, msg);
    }

    public static ResultEntity error(int code, String msg) {
        ResultEntity r = new ResultEntity();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static ResultEntity ok(String msg) {
        ResultEntity r = new ResultEntity();
        r.put("msg", msg);
        return r;
    }

    public static ResultEntity ok(Map<String, Object> map) {
        ResultEntity r = new ResultEntity();
        r.putAll(map);
        return r;
    }

    public static ResultEntity ok() {
        return new ResultEntity();
    }

    public String toJSONString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", this.get("code"));
        jsonObject.put("msg", this.get("msg"));
        return jsonObject.toString();
    }

    @Override
    public ResultEntity put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
